package com.njwb.day15.zqzb.sql;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleLogin {
    public static void main(String[] args) {
        // 创建窗口
        JFrame frame = new JFrame("登录界面");
        frame.setSize(300, 180);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null); // 居中显示

        // 创建面板
        JPanel panel = new JPanel();
        frame.add(panel);
        placeComponents(panel);

        // 设置可见
        frame.setVisible(true);
    }

    private static void placeComponents(JPanel panel) {
        panel.setLayout(null); // 绝对布局

        // 用户名标签
        JLabel userLabel = new JLabel("用户名:");
        userLabel.setBounds(10, 20, 80, 25);
        panel.add(userLabel);

        // 用户名文本框
        JTextField userText = new JTextField(20);
        userText.setBounds(100, 20, 165, 25);
        panel.add(userText);

        // 密码标签
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(10, 50, 80, 25);
        panel.add(passwordLabel);

        // 密码输入框
        JPasswordField passwordText = new JPasswordField(20);
        passwordText.setBounds(100, 50, 165, 25);
        panel.add(passwordText);

        // 登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setBounds(100, 90, 80, 25);
        panel.add(loginButton);

        // 登录按钮事件
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String user = userText.getText();
                String password = new String(passwordText.getPassword());

                // 简单的验证逻辑（用户名：admin，密码：123456）
                if (user.equals("admin") && password.equals("123456")) {
                    JOptionPane.showMessageDialog(panel, "登录成功！");
                } else {
                    JOptionPane.showMessageDialog(panel, "用户名或密码错误！");
                }
            }
        });
    }
}
